package ysq.xyg.web.service.impl;

import cn.hutool.core.util.ObjectUtil;
import cn.hutool.core.util.StrUtil;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.github.pagehelper.StringUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import tk.mybatis.mapper.entity.Example;
import ysq.xyg.base.model.dto.ReturnEntity;
import ysq.xyg.base.service.impl.BaseServiceImpl;
import ysq.xyg.base.util.BaseConstant;
import ysq.xyg.sys.model.po.SysUser;
import ysq.xyg.web.dao.WebShareDao;
import ysq.xyg.web.model.po.WebShare;
import ysq.xyg.web.model.vo.WebShareVo;
import ysq.xyg.web.service.WebShareService;

import javax.annotation.Resource;
import java.util.Date;
import java.util.List;

/**
 * 业务模块-微信分享service实现
 *
 * @author shengQiang yu
 * @date 2019-5-17 10:29:41
 */
@Service("webShareService")
public class WebShareServiceImpl extends BaseServiceImpl<WebShare> implements WebShareService {

    @Resource
    private WebShareDao webShareDao;

    private WebShare webShare;

    @Autowired
    public void setBaseDao(WebShareDao webShareDao) {
        this.baseDao = webShareDao;
    }

    /**
     * 分页查询
     *
     * @param webShareVo 对象
     * @param pageSize 展示数量
     * @param pageNum 当前页面
     * @return ReturnEntity 对象
     * @author shengQiang yu
     * @date 2019-5-17 10:29:41
     */
    @Override
    public ReturnEntity page(WebShareVo webShareVo, int pageSize, int pageNum) {
        PageHelper.startPage(pageNum,pageSize);
        PageHelper.orderBy(" update_time desc");
        List<WebShare> list = acceptList(webShareVo);
        PageInfo<WebShare> page=new PageInfo<WebShare>(list);
        return new ReturnEntity(page.getList(),page.getTotal(),page.isHasNextPage());
    }

    /**
     * 修改
     *
     * @param webShareVo 对象
     * @param currentUser 当前用户
     * @return ReturnEntity 对象
     * @author shengQiang yu
     * @date 2019-5-17 10:29:41
     */
    @Override
    public ReturnEntity addOrUpdate(WebShareVo webShareVo, SysUser currentUser) {
        boolean isUpate=true;
        if(StrUtil.isEmpty(webShareVo.getId())){
            webShare=new WebShare(currentUser);
            isUpate=false;
        }else{
            webShare=selectByPrimaryKey(webShareVo.getId());
            if(ObjectUtil.isNull(webShare)){
                return new ReturnEntity(BaseConstant.FAIL, BaseConstant.DO_FAIL);
            }
            webShare.setUpdateTime(new Date());
            webShare.setUpdateUser(currentUser.getId());
        }
        webShare.setShareTitle(webShareVo.getShareTitle());
        webShare.setShareDesc(webShareVo.getShareDesc());
        webShare.setSharePic(webShareVo.getSharePic());
        webShare.setShareUrl(webShareVo.getShareUrl());
        int num=0;
        if(isUpate){
            num=updateByPrimaryKey(webShare);
        }else{
            num=insert(webShare);
        }
        if(num<=0){
            return new ReturnEntity(BaseConstant.FAIL, BaseConstant.DO_FAIL);
        }
        return new ReturnEntity();
    }

    /**
     * 删除
     *
     * @param id 主键id
     * @param currentUser 当前用户
     * @return ReturnEntity 对象
     * @author shengQiang yu
     * @date 2019-5-17 10:29:41
     */
    @Override
    public ReturnEntity delete(String id, SysUser currentUser) {
        WebShare webShare=selectByPrimaryKey(id);
        if(webShare!=null){
            webShare.setRemoveTag(BaseConstant.BASE_REMOVE_TAG_DELETE);
            webShare.setUpdateTime(new Date());
            webShare.setUpdateUser(currentUser.getId());
            int num=updateByPrimaryKey(webShare);
            if(num>0){
                return new ReturnEntity();
            }
        }
        return new ReturnEntity(BaseConstant.FAIL, BaseConstant.DO_FAIL);
    }

    /**
     * 获取集合
     *
     * @param webShareVo 对象
     * @return List<BaseDict> BaseDict集合
     * @author shengQiang yu
     * @date 2019-5-17 10:29:41
     */
    @Override
    public List<WebShare> acceptList(WebShareVo webShareVo){
        Example example = new Example(WebShare.class);
        Example.Criteria criteria = example.createCriteria();
        if(StringUtil.isNotEmpty(webShareVo.getRemoveTag())){
            criteria.andEqualTo("removeTag",webShareVo.getRemoveTag());
        }
        example.setOrderByClause(" update_time desc");
        return webShareDao.selectByExample(example);
    }
}
